package com.baeldung._01_generics;

import lombok.Data;

import java.io.Serializable;
import java.util.Optional;

/**
 * 泛型实体
 * @param <T>
 */
@Data
public class GenericEntry<T> {
    private T data;
    private int rank;

    // non-generic constructor
    public GenericEntry(int rank) {
        this.rank = rank;
    }

    // generic constructor
    public GenericEntry(T data, int rank) {
        try {
            Object o = data.getClass().newInstance();
            System.out.println("o = " + o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.data = data;
        this.rank = rank;
    }

    // generic constructor with different type

    /**
     * 不同类型的泛型构造器 => 本质上还是相同的类型
     * Unchecked cast: 'E' to 'T' 未经检查的转换：从“ E”到“ T”
     * @param element 对象
     * @param <E> 泛型构造方法的类型
     */
    public <E extends Rankable & Serializable> GenericEntry(E element) {
        this.data = (T) element;
        this.rank = element.getRank();
    }

    // generic constructor with different type and wild card 具有不同类型和通配符的通用构造函数
    public GenericEntry(Optional<? extends Rankable> optional) {
        if (optional.isPresent()) {
            this.data = (T) optional.get();
            this.rank = optional.get()
                .getRank();
        }
    }

    public static void main(String[] args) {

        GenericEntry<Object> objectGenericEntry = new GenericEntry<>(new Object(), 1);

    }


}
